﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
namespace HZ.DrugTrade.Web.Admin.SystemManage
{
    public partial class SortEditManage : BasePage
    {
        private string McID = "-1";
        IBLL_Sort ibbs = Factory.IBLL_Sort();
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                if (this.Page.Request.QueryString["ID_ID"] != string.Empty && this.Page.Request.QueryString["ID_ID"] != null)
                {
                    this.McID = this.Page.Request.QueryString["ID_ID"].ToString();//当前节点的id
                    ViewState["IDCount"] = this.Page.Request.QueryString["IDCount"].ToString();//当前节点的同级节点俄个数
                    ViewState["IDson"] = Request.QueryString["IDson"].ToString();//当前节点子点击个数
                }

                //首次进入页面
                if (!this.IsPostBack)
                {
                    //为删除按钮添加确认脚本
                    //  Utility.ClientAPI.AddButtonConfirm(btnDelete, "确实要删除分类吗？");


                    if (this.McID != "-1")
                    {

                        Sort sort = ibbs.GetSort(McID.ToString());
                        this.Txtname.Text = sort.SortName;               //保存当前节点名字
                        this.HsortID.Value = sort.SortID;                //保存当前节点ID
                        TxtId.Text = sort.SortID;
                        this.HsortID_ParentID.Value = sort.ParentSortID; //保存父节点ID
                    }
                }
            }
            catch (Exception ex)
            {
                //Utility.Message.Show(ex.Message);
            }
        }





        /// <summary>
        /// 添加类别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddRoot_Click(object sender, EventArgs e)
        {
            PublicFactory(1);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click1(object sender, EventArgs e)
        {
            PublicFactory(4);
        }

        /// <summary>
        /// 操作公共方法
        /// </summary>
        /// <param name="ntype">1 添加同级 2 修改 3 添加子 4 删除 5 提升</param>
        private void PublicFactory(int ntype)
        {
            try
            {
                int nResoult = 0;
                string _strAlert = "";
                Sort sort = new Sort();

                switch (ntype)
                {
                    case 1:     //添加同级
                        sort.SortName = this.Txtname.Text.Trim();
                        sort.SortID = (Convert.ToInt32(ViewState["IDCount"].ToString()) + 1).ToString();
                        sort.ParentSortID = HsortID_ParentID.Value; //根分类
                        nResoult = ibbs.AddSort(sort);
                        _strAlert = "新增主分类";
                        break;
                    case 2:     //修改
                        sort.SortName = this.Txtname.Text.Trim();
                        sort.SortID = HsortID.Value;
                        sort.ParentSortID = HsortID_ParentID.Value; //根分类
                        nResoult = ibbs.UpdateSort(sort);
                        _strAlert = "修改";
                        break;
                    case 3:     //添加子
                        sort.SortName = this.Txtname.Text.Trim();
                        sort.SortID = (Convert.ToInt32(ViewState["IDson"].ToString()) + "01").ToString();
                        sort.ParentSortID = HsortID.Value; //根分类
                        nResoult = ibbs.AddSort(sort);
                        _strAlert = "新增子分类";
                        break;
                    case 4:     //删除带子节点
                        sort.SortID = HsortID.Value;
                        sort.ParentSortID = HsortID_ParentID.Value; //根分类
                        nResoult = ibbs.DeleteSortSon(sort, 2);
                        _strAlert = "删除";
                        break;
                    case 5:     //提升子节点
                        sort.SortID = HsortID.Value;
                        sort.ParentSortID = HsortID_ParentID.Value; //根分类
                        nResoult = ibbs.UPSortSon(sort);

                        _strAlert = "提升";
                        break;

                }
                if (nResoult > 0)
                {
                    Response.Write("<script>alert('" + _strAlert + "成功');parent.location='SortManage.aspx?ID_ID=" + HsortID_ParentID.Value + "';</script>");
                    //Utility.Message.ResponseScript("alert('主分类添加成功!');parent.location='SortManage.aspx'");
                }
                else
                {
                    PageError("" + _strAlert + "失败！", "");
                }


            }
            catch (Exception ex)
            {
                // Utility.Message.Show(ex.Message);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnModify_Click1(object sender, EventArgs e)
        {
            PublicFactory(2);
        }
        /// <summary>
        /// 添加子类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddChild_Click1(object sender, EventArgs e)
        {
            PublicFactory(3);
        }
        /// <summary>
        /// 提升
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete0_Click(object sender, EventArgs e)
        {
            PublicFactory(5);
        }
     
    }
}
